package ch.il07.converter.persistance.tests;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

import ch.il07.converter.persistence.Persistence;
import ch.il07.converter.persistence.PersistenceHelper;
import ch.il07.converter.persistence.entities.AccessLevel;
import ch.il07.converter.persistence.entities.Measurand;
import ch.il07.converter.persistence.entities.Unit;
import ch.il07.converter.persistence.entities.User;
import ch.il07.converter.persistence.exceptions.AlreadyDefinedException;
import ch.il07.converter.persistence.exceptions.PersistenceException;
import ch.il07.converter.persistence.exceptions.UserAlreadyExistsException;
import ch.il07.utils.RandomHelper;

public class DeleteUser {

	private Persistence persistence;

	@Before
	public void setUp() {
		try {
			persistence = PersistenceHelper.getPersistence();
		} catch (PersistenceException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void deleteUserWithoutStuff() {
		String username = RandomHelper.randString();
		User u = null;

		try {
			persistence.addUser(username, RandomHelper.getEmail(username), username,
					AccessLevel.USER);
			u = persistence.getUser(username, username);
			assertNotNull("user '" + username + "' should exist in db", u);
		} catch (UserAlreadyExistsException e) {
			fail(e.getMessage());
		} catch (PersistenceException e) {
			fail(e.getMessage());
		}

		try {
			persistence.deleteUser(u);
		} catch (PersistenceException e) {
			fail(e.getMessage());
		}

		try {
			assertNull("user '" + username + "' should be removed", persistence.getUser(
					username, username));
		} catch (PersistenceException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void deleteUserWithStuff() {
		String username = RandomHelper.randString();
		Measurand m = new Measurand(RandomHelper.randString(),new Unit(RandomHelper.randString(), 1D));
		m.addUnit(new Unit(RandomHelper.randString(), 3.3, 2.2));

		{ // add user into db
			try {
				persistence.addUser(username, RandomHelper.getEmail(username), username,
						AccessLevel.USER);
				assertNotNull("user '" + username + "' should exist in db", persistence
						.getUser(username, username));
			} catch (UserAlreadyExistsException e) {
				fail(e.getMessage());
			} catch (PersistenceException e) {
				fail(e.getMessage());
			}
		}

		{ // add measurand with units
			try {
				persistence.addMeasurand(m, username);
							
				List<Measurand> measurands = persistence.getMeasurands(username);

				boolean measurandIsAvailable = false;
				for (Measurand measurand : measurands) {
					if (measurand.getName().equals(m.getName())) {
						measurandIsAvailable = true;
						break;
					}
				}
				assertTrue("measurand '" + m.getName() + "' should exist in db",
						measurandIsAvailable);
			} catch (AlreadyDefinedException e) {
				fail(e.getMessage());
			} catch (PersistenceException e) {
				fail(e.getMessage());
			}
		}

		{ // delete user
			try {
				persistence.deleteUser(new User(username, username, RandomHelper.getEmail(username)));
			} catch (PersistenceException e) {
				fail(e.getMessage());
			}
		}
	}

}
